﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Ajax.Utilities;
using VoteMS.BLL;

namespace VoteMS.UI
{


    /// <summary>
    /// ProjectManager 的摘要说明
    /// </summary>
    public class ProjectManager : IHttpHandler
    {
        private  readonly  ProjectsBLL projectsBll = new ProjectsBLL();
        private  readonly  VoteRecordBLL voteRecordBll = new VoteRecordBLL();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request.QueryString["type"];
            switch (type)
            {
                case "sel":
                    GetProjectListJson(context);
                    break;
                case "add":
                    break;
                case "mac":
                    context.Response.Write(voteRecordBll.GetRecordByMy());
                    break;
                case "vote":
                    AddVoteRecords(context);
                    break;
            }
        }

        private void AddVoteRecords(HttpContext context)
        {
            int pid = int.Parse(context.Request.Form["pid"]);
            string voted = context.Request.Form["voted"];
            string result = voteRecordBll.AddVoteRecord(pid, voted);
            context.Response.Write(result);
        }

        private void GetProjectListJson(HttpContext context)
        {
            int projectType =int.Parse( context.Request.QueryString["projectType"]);
            string list =  projectsBll.GetProjectListJson(projectType);
            context.Response.Write(list);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}